/**
  ******************************************************************************
  * File Name          : weight.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "tskcfg.h"
#include "cf.h"
#include "route_expio_485.h"
#include <string.h>

const Route_DevType_Type Route_LocalDevType = RT_DV_CF;

static void route_expio_cf_up_state(Route_PayloadType *outpl)
{
  if(memcmp((void *)&Cf_State, &Cf_State_last, sizeof(Cf_StateType)) != 0)
  {
    outpl->trans_dir = ROUTE_TRSDIR_SUBDEV_TO_NET;
    outpl->pl_type   = ROUTE_PLTYP_CF_CYCLE;
    outpl->pl_len    = sizeof(Cf_StateType);
    *((Cf_StateType *)(outpl->payload)) = Cf_State;
    
    Cf_State_last = Cf_State;
  }
  else
  {
    outpl->trans_dir = ROUTE_TRSDIR_SUBDEV_TO_LOCAL;
    outpl->pl_type   = ROUTE_PLTYP_CF_ACK;
    outpl->pl_len    = 0;
  }
}

static void route_expio_cf_up_sync(Route_PayloadType *outpl)
{
  outpl->trans_dir = ROUTE_TRSDIR_SUBDEV_TO_NET;
  outpl->pl_type   = ROUTE_PLTYP_CF_SYNC;
  outpl->pl_len    = sizeof(Cf_SyncType);
 
  ((Cf_SyncType *)(outpl->payload))->hour   = Sys_Runtime.hour;
  ((Cf_SyncType *)(outpl->payload))->minute = Sys_Runtime.minute;
  ((Cf_SyncType *)(outpl->payload))->second = Sys_Runtime.second;
  ((Cf_SyncType *)(outpl->payload))->state  = Cf_State;
  ((Cf_SyncType *)(outpl->payload))->para   = Cf_RunPara;
}

uint8_t Route_expio_xx_rxprocess(Route_PayloadType *inpl, Route_PayloadType *outpl)
{
  uint8_t sftype;
  uint8_t err = 0;
  
  switch(inpl->pl_type)
  {
    case ROUTE_PLTYP_CF_CYCLE:
      route_expio_cf_up_state(outpl);
      break;
    
    case ROUTE_PLTYP_CF_PARA:
      if(inpl->pl_len != sizeof(Cf_SyncType))
      {
        return 1;
      }
      
      if(memcmp(&Cf_RunPara, &(((Cf_SyncType *)(inpl->payload))->para), sizeof(Cf_ParaType)) != 0)
      {
        if(Cf_ParaCheck(&(((Cf_SyncType *)(inpl->payload))->para)) != 0)
        {
          return 3;
        }
        
        Cf_RunPara = ((Cf_SyncType *)(inpl->payload))->para;
        
        if(Storage_qhd != 0)
        {
          sftype = 1;
          xQueueSend(Storage_qhd, &sftype, 0);
        }
      }
      
      Sys_Adjtime.hour   = ((Cf_SyncType *)(inpl->payload))->hour;
      Sys_Adjtime.minute = ((Cf_SyncType *)(inpl->payload))->minute;
      Sys_Adjtime.second = ((Cf_SyncType *)(inpl->payload))->second;
      Sys_SetDataTime();
      
      route_expio_cf_up_sync(outpl);
      break;
    
    case ROUTE_PLTYP_CF_SYNC:
      route_expio_cf_up_sync(outpl);
      break;
    
    case ROUTE_PLTYP_CF_DOACT:
      if(inpl->pl_len != sizeof(Cf_ActType))
      {
        return 1;
      }
      
      if(Cf_RxCmd_Qhd == NULL)
      {
        return 4;
      }
      
      if(((Cf_ActType *)(inpl->payload))->cfkg == 1)
      {
        xQueueSend(Cf_RxCmd_Qhd, &Cf_SendCmdOn, 0);
      }
      else if(((Cf_ActType *)(inpl->payload))->cfkg == 0)
      {
        xQueueSend(Cf_RxCmd_Qhd, &Cf_SendCmdOff, 0);
      }
      else
      {
        return 3;
      }
      
      route_expio_cf_up_state(outpl);
      break;
    
    default:
      return 2;
  }
  
  return err;
}
